<?php

class Common_Controller_Front extends Common_Controller_Main {

    public $rola = 'quest';

    /**
     *
     * @var Common_Controller_Helper_Content 
     */
    public $cms = null;

    /**
     *
     * @var Application_Model_Content
     */
    public $contentModel = null;

    /**
     *
     * @var Application_Model_Categories
     */
    public $categoriesModel = null;

    /**
     *
     * @var string Tag for content items to load boxes
     */
    public $boxTag = '*';

    public function init() {
        parent::init();

        $this->boxTag = $this->createBoxTag();

        $this->addCssFile('jquery-ui.css');
        $this->addCssFile('main-front.css');


        $this->cms = $this->getContentHelper();

        $set = new Application_Model_Settings();
        $set->define();

        $this->contentModel = new Application_Model_Content();
        $this->categoriesModel = new Application_Model_Categories();

        $this->view->sliderItems = $this->cms->getContent(
                Application_Model_Content::TYP_SLIDETOP, null, Application_Model_Content::BX_SLIDER
        );

        if ($this->isHome()) {
            $this->view->contentItems = $this->contentModel->getContent('isHome=1 and typ=' . Application_Model_Content::TYP_CONTENT . ' and boxName like \'' . Application_Model_Content::BX_CENTER . '\'');
        } elseif ($this->_request->getActionName() == "content") {
            $this->view->contentItems = $this->contentModel->getContent('id=' . $this->getParam('id', 0));
        } elseif ($this->_request->getActionName() == "category") {
            $this->view->contentItems = $this->contentModel->getContentByCategory((int) $this->getParam('id', 0));
        }

        $this->view->topBoxItems = $this->cms->getContent(
                array(Application_Model_Content::TYP_CONTENT, Application_Model_Content::TYP_TEXTSHORT), null, Application_Model_Content::BX_HTOP
        );

        $this->view->leftBoxItems = $this->cms->getContent(
                array(Application_Model_Content::TYP_CONTENT, Application_Model_Content::TYP_TEXTSHORT), null, Application_Model_Content::BX_LEFT, $this->boxTag, $this->isHome() ? 'isHome=1' : null
        );
        $this->view->rightBoxItems = $this->cms->getContent(
                array(Application_Model_Content::TYP_CONTENT, Application_Model_Content::TYP_TEXTSHORT), null, Application_Model_Content::BX_RIGHT
        );
        $this->view->footBoxItems = $this->cms->getContent(
                array(Application_Model_Content::TYP_CONTENT, Application_Model_Content::TYP_TEXTSHORT), null, Application_Model_Content::BX_FOOT
        );

        $this->view->menuItems = $this->categoriesModel->getCategories(array(
            'where' => 'isHome=1 and typ=' . Application_Model_Categories::T_MENU . '',
                ));
    }

    public function createBoxTag() {

        $params = $this->getAllParams();
        $p = $params['module'] . ',' . $params['controller'] . ',' . $params['action'] . '' . (isset($params['id']) ? ',' . $params['id'] : '');

        $tag = preg_replace('#default,|index,?#', '', $p);


        return $tag != "" ? $tag : '*';
    }

    /**
     * 
     * @return bool
     */
    public function isHome() {
        $request = $this->_request;
        return $request->getModuleName() == "default" && $request->getControllerName() == "default" || $request->getActionName() == "index";
    }

}